Unity Games by Tutorials by unknow

Unity Games by Tutorials by unknow

Author:unknow
Language: eng
Format: epub
Publisher: Ray Wenderlich


In the Project Browser, right-click the Scripts folder and create a new C# Script. Name it SoyBoyController. Double-click it to open the script in your editor.

At the top of the script, just above the public class SoyBoyController definition, add the following:

[RequireComponent(typeof(SpriteRenderer), typeof(Rigidbody2D), typeof(Animator))]

This line validates that any GameObject attached to this script has the minimum necessary components required to work — it basically pre-qualifies the components. Your SoyBoy GameObject meets the requirement so it will pass with flying colors.

This is a handy script-writing trick that ensures the script only applies to GameObjects that meet the requirements.

Now you need property fields that hold useful values and cache component references for later use. Add the following variables inside the SoyBoyController definition but above the Start():

public float speed = 14f; public float accel = 6f; private Vector2 input; private SpriteRenderer sr; private Rigidbody2D rb; private Animator animator;

The speed and accel floats hold pre-defined values to use when calculating how much force to apply to Super Soy Boy’s Rigidbody. You make these public so you can easily tweak them in the editor; for instance, you could use them to change the feel of the controls.

The Vector2 input field stores the controller’s current input values for x and y at any point in time. Negatives mean the controls are going left (-x) or down (-y), and positives mean right (x) or up (y).

The last three private fields cache references to the SpriteRenderer, Rigidbody2D and Animator components. These prevent you from having to find these references repeatedly during the Update() method loop. This approach simply prevents unnecessary variable assignment iterations that could negatively impact performance.

Now, you’ll create a new method called Awake(). Add the following just above the existing Start() method.

void Awake() { sr = GetComponent<SpriteRenderer>(); animator = GetComponent<Animator>(); rb = GetComponent<Rigidbody2D>(); }

In short, this ensures component references are cached when the game starts.

More specifically, it locates the specified component types on the GameObject upon which SoyBoyController.cs sits and assigns them to the three fields. With this, you reap the benefits of using the RequireComponent() class attribute.

To bring it full circle, consider this: If you didn’t enforce required component types and the GameObject didn’t have one or more of these components, then Awake() would fail when it tried to locate the missing component(s)!

Now to make sure Super Soy Boy knows what direction to face. You’ll use Unity’s Input class to interpret user input and so you can change the direction Super Soy Boy’s sprite faces.

Add the following code to Update():

void Update() { // 1 input.x = Input.GetAxis("Horizontal"); input.y = Input.GetAxis("Jump"); // 2 if (input.x > 0f) { sr.flipX = false; } else if (input.x < 0f) { sr.flipX = true; } }

Input.GetAxis() gets X and Y values from the built-in Unity control axes named Horizontal and Jump. These values will either be -1, 0 or 1, depending on whether the controls are left, right, up, down or neutral. The value is held in the x and y properties of the input variable, which is a Vector2.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.